package com.flycms.web.front.rest;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.flycms.core.base.BaseController;
import com.flycms.core.entity.PagingParam;
import com.flycms.core.entity.RestDataVo;
import com.flycms.core.utils.web.utils.CommonUtils;
import com.flycms.module.soft.model.Case;
import com.flycms.module.soft.model.Soft;
import com.flycms.module.solution.model.Solution;
import com.flycms.module.solution.model.SolutionFunction;
import com.flycms.module.solution.model.SolutionPartner;
import com.flycms.module.solution.service.SolutionFunctionService;
import com.flycms.module.solution.service.SolutionPartnerService;
import com.flycms.module.solution.service.SolutionService;

/**
 *
 * <br/>Copyright 2020
 * <br/>All rights reserved
 * @author 沁言訫語
 * @version 2020年3月10日
 * @Date 2020年3月10日
 */

@RestController
@RequestMapping("/solution")
public class SolutionController extends BaseController {
    
    private static final Logger logger = LogManager.getLogger(SolutionController.class);
    
    @Autowired
    protected SolutionService solutionService;
    
    @Autowired
    protected SolutionFunctionService solutionFunctionService;
    
    @Autowired
    protected SolutionPartnerService solutionPartnerService;
    
    @GetMapping(value = {"/list/{categoryId}"})
    public RestDataVo getSolutionList(@PathVariable("categoryId") Integer categoryId,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
            @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        PagingParam paging = CommonUtils.validParam(pageIndex, pageSize);
        RestDataVo rest = null;
        try {
            List<Solution> solutionList = solutionService.getSolutionList(categoryId, title,
                    paging.getOffset(), paging.getRows());
            int count = solutionService.getSolutionCount(categoryId, title);
            rest = RestDataVo.PAGE(solutionList, paging.getPageIndex(), paging.getPageSize(), count);
        } catch (Exception e) {
            logger.error("", e);
            rest = RestDataVo.ERROR("获取解决方案列表异常");
        }
        return rest;
    }
    
    @GetMapping(value = {"/info/{id}"})
    public RestDataVo getSolutionInfo(@PathVariable("id") Integer id) {
        RestDataVo rest = null;
        return rest;
    }
    
    @GetMapping(value = {"/f/list/{solutionId}"})
    public RestDataVo getSolutionFunctionList(@PathVariable("solutionId") Integer solutionId,
            @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
            @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        PagingParam paging = CommonUtils.validParam(pageIndex, pageSize);
        RestDataVo rest = null;
        try {
            List<SolutionFunction> list = solutionFunctionService.getSolutionFunctionList(solutionId,
                    paging.getOffset(), paging.getRows());
            int count = solutionFunctionService.getSolutionFunctionCount(solutionId);
            rest = RestDataVo.PAGE(list, paging.getPageIndex(), paging.getPageSize(), count);
        } catch (Exception e) {
            logger.error("", e);
            rest = RestDataVo.ERROR("获取方案功能异常");
        }
        return rest;
    }
    
    @GetMapping(value = {"/p/list/{solutionId}"})
    public RestDataVo getSolutionPartnerList(@PathVariable("solutionId") Integer solutionId,
            @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
            @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        PagingParam paging = CommonUtils.validParam(pageIndex, pageSize);
        RestDataVo rest = null;
        try {
            List<SolutionPartner> list = solutionPartnerService.getSolutionPartnerList(solutionId,
                    paging.getOffset(), paging.getRows());
            int count = solutionPartnerService.getSolutionPartnerCount(solutionId);
            rest = RestDataVo.PAGE(list, paging.getPageIndex(), paging.getPageSize(), count);
        } catch (Exception e) {
            logger.error("", e);
            rest = RestDataVo.ERROR("获取方案合作伙伴异常");
        }
        return rest;
    }
    
    @GetMapping(value = {"/s/list/{solutionId}"})
    public RestDataVo getSolutionStructList(@PathVariable("solutionId") Integer solutionId,
            @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
            @RequestParam(value = "pageSize", required = false) Integer pageSize) {
         PagingParam paging = CommonUtils.validParam(pageIndex, pageSize);
        RestDataVo rest = null;
        try {
            List<Soft> softList = solutionService.getSolutionProduct(solutionId, paging.getOffset(), paging.getRows());
            int count = solutionService.getSolutionProductCount(solutionId);
            rest = RestDataVo.PAGE(softList, paging.getPageIndex(), paging.getPageSize(), count);
        } catch (Exception e) {
            logger.error("", e);
            rest = RestDataVo.ERROR("获取方案架构异常");
        }
        return rest;
    }
    
    @GetMapping(value = {"/c/list/{solutionId}"})
    public RestDataVo getSolutionCaseList(@PathVariable("solutionId") Integer solutionId,
            @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
            @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        PagingParam paging = CommonUtils.validParam(pageIndex, pageSize);
        RestDataVo rest = null;
        try {
            List<Case> caseList = solutionService.getSolutionCase(solutionId, paging.getOffset(), paging.getRows());
            int count = solutionService.getSolutionCaseCount(solutionId);
            rest = RestDataVo.PAGE(caseList, paging.getPageIndex(), paging.getPageSize(), count);
        } catch (Exception e) {
            logger.error("", e);
            rest = RestDataVo.ERROR("获取方案用户案例异常");
        }
        return rest;
    }
    
}
